#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include <QPixmap>
#include <QDebug>
#include <QTimer>
#include <QSound>

#include "mypushbutton.h"


MainScene::MainScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainScene)
{
    ui->setupUi(this);
    setWindowTitle("翻金币");
    setWindowIcon(QIcon(":/title_icon.png"));

    //设置固定大小
    this->setFixedSize(320, 583);
    //resize(320, 583); //设置窗口大小

    connect(ui->actionexit, &QAction::triggered, [=](){
        this->close();
    });

    //声音
    QSound *startSound = new QSound(":/TapButtonSound.wav", this);
    //startSound->setLoops(2);播放次数，如果为-1的话 无限播放

    //开始按键创建
    MyPushButton* startBtn = new MyPushButton(":/MenuSceneStartButton");
    startBtn->setParent(this);
    startBtn->move((this->width() - startBtn->width()) * 0.5, this->height() * 0.7);

    m_levelScene = new ChooseLevelScene();
    connect(startBtn, &MyPushButton::clicked, [=](){
        //qDebug() << "点击了按键";

        //播放开始有效
        startSound->play();

        startBtn->zoom1();
        startBtn->zoom2();

        //延时进入到第二个场景中
        //参数1：延时时间  参数2：谁执行  参数3：执行什么函数
        QTimer::singleShot(300, this, [=](){

            //将自身隐藏掉
            this->hide();

            //进入到第二个 选择关卡场景中
            //设置选择关卡场景的位置  与当前场景位置相同
            m_levelScene->setGeometry(this->geometry());
            m_levelScene->show();
        });
    });


    connect(m_levelScene, &m_levelScene->hided, [=](){

        QTimer::singleShot(300, this, [=](){

            //隐藏选择关卡场景中
            m_levelScene->hide();

            //将自身隐藏掉
            this->show();
        });
    });

}


void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter *painter = new QPainter(this);
    QPixmap pix;

    //添加背景
    pix.load(":/PlayLevelSceneBg.png");
    painter->drawPixmap(0, 0, this->width(), this->height(), pix);

    //添加logo
    pix.load(":/main_logo.png");
    pix = pix.scaled(pix.width() * 0.3, pix.height() * 0.3);// 缩放图片
    painter->drawPixmap(10, 30, pix);
}


MainScene::~MainScene()
{
    delete ui;
}
